//待补充
$NG.AllReady(function (
    editPage,
    { useAction, useValuesChange, useUpdateRow, useDataIndexChange,useUpdateRows , useClick }
) {
    const profit = $NG.getCmpApi("p_form_detailed_design_d")
    const headerTable = $NG.getCmpApi("p_form_detailed_design")
   useValuesChange(({args})=>{
    console.log(`项目监听`,args);
    $NG.updateState((updater)=>{
        updater.data.p_form_detailed_design.setProps({
            u_cnt_name:{
                label:"",
                value:""
            },
            u_cnt_no:{
                label:"",
                value:""
            }
        })
    })
    filterCntMain()
    getProject()
   }, "p_form_detailed_design.phid_pc")
   useValuesChange(({args})=>{
    console.log(`合同信息`,args);
    $NG.updateState((updater)=>{
        updater.data.p_form_detailed_design.setProps({
            u_cnt_no:{
                label:args[0].u_cnt_name.origin.bill_no,
                value:args[0].u_cnt_name.origin.bill_no
            }
        })
    })
    getCntInfo()
   }, "p_form_detailed_design.u_cnt_name")
   // 监听是否影响工期
   var mstform = $NG.getCmpApi('p_form_detailed_design');
   useValuesChange(({args})=>{
    console.log(`工期监听`,args);
    const _value = headerTable.getItem("u_is_impact_schedule").getValue()
    console.log(`是否影响工期`, _value);
    let lock = false
    if(Number(_value) == 1){
        lock = true
    } else {
        lock = false
    }
    $NG.updateUI(function (updater, state) {
          updater.fieldSetForm.p_form_detailed_design.u_is_adjust_schedule.setProps({
            required: lock, //required是否必输
          });
        });
   }, "p_form_detailed_design.u_is_impact_schedule")

   // 监听下方新增表格
   useUpdateRow(({args})=>{
    console.log("表格监听",args);
    // 合同数量
    const u_cnt_qty = args[0].u_cnt_qty
    // 拟分包单价
    const u_sub_prc = args[0].u_sub_prc
    // 拟采购单价
    const u_pur_prc = args[0].u_pur_prc
    // 自制单价
    const u_self_prc = args[0].u_self_prc
    // 拟分包成本
    args[0].u_sub_cost = Number(u_cnt_qty)*Number(u_sub_prc)
    // 你采购成本
    args[0].u_pur_cost = Number(u_cnt_qty)*Number(u_pur_prc)
    // 自制成本
    args[0].u_self_cost = Number(u_cnt_qty)*Number(u_self_prc)
    
    profit.refreshView()
   },"p_form_detailed_design_d")

   // 监听表格的所有变化
   useUpdateRows(({args})=>{
    console.log("表格全部变化监听", args);
    // 设备清单拟分包成本
    let _u_equipment_list_sub_cost = 0
    // 设备清单拟采购成本
    let _u_equipment_list_pur_cost = 0;
    // 设备清单自制成本
    let _u_equipment_list_self_cost = 0;
    // 深化拟分包
    let _u_detailed_sub_cost = 0;
    // 深化拟采购
    let _u_detailed_pur_cost = 0;
    // 深化自制成本
    let _u_detailed_self_cost = 0;
    args[0].forEach(element => {
        _u_equipment_list_sub_cost+= Number(element.u_sub_cost)? Number(element.u_sub_cost):0
        _u_equipment_list_pur_cost+= Number(element.u_pur_cost)?Number(element.u_pur_cost):0
        _u_equipment_list_self_cost+= Number(element.u_self_cost)?Number(element.u_self_cost):0
        // 深化
        _u_detailed_sub_cost+=(Number(element.u_current_detailed_qty)?Number(element.u_current_detailed_qty):0)*(Number(element.u_sub_prc)?Number(element.u_sub_prc):1)
        _u_detailed_pur_cost += Number(element.u_current_detailed_qty)*Number(element.u_pur_prc)
        _u_detailed_self_cost+=Number(element.u_current_detailed_qty)*Number(element.u_self_prc)
    });
    $NG.updateState((updater)=>{
        updater.data.p_form_detailed_design.setProps({
            u_equipment_list_sub_cost: _u_equipment_list_sub_cost,
            u_equipment_list_pur_cost: _u_equipment_list_pur_cost,
            u_equipment_list_self_cost: _u_equipment_list_self_cost,
            u_detailed_sub_cost: _u_detailed_sub_cost,
            u_detailed_pur_cost: _u_detailed_pur_cost,
            u_detailed_self_cost: _u_detailed_self_cost,
            // 设备清单总额
            u_equipment_list_total_cost: _u_equipment_list_sub_cost+_u_equipment_list_pur_cost+_u_equipment_list_self_cost,
            u_detailed_total_cost: _u_detailed_sub_cost+_u_detailed_pur_cost+_u_detailed_self_cost,
        })
    })
   },"p_form_detailed_design_d")
   // 自定义监听区域
   // 存放临时对比值，提高性能
   var _map={}
   // 监听合同金额
   const cntTotal= ()=>{
        const total = Number(headerTable.getItem("u_equipment_list_total_cost").getValue())
        const CntNum = Number(headerTable.getItem("u_cnt_revenue").getValue())
        // 深化
        const SenPrice = Number(headerTable.getItem("u_detailed_design_revenue").getValue())
        const SenPrice_d = Number(headerTable.getItem("u_detailed_total_cost").getValue())
        $NG.updateState((updater)=>{
            updater.data.p_form_detailed_design.setProps({
                u_signed_gp:CntNum - total,
                u_signed_gm:((CntNum - total)/CntNum).toFixed(2),
                u_detailed_gp:SenPrice - SenPrice_d,
                u_detailed_gm:((SenPrice - SenPrice_d)/SenPrice).toFixed(2)
            })
        })
   }
   // 监听合同成本
   EventHideTable("u_cnt_revenue",cntTotal)
   // 监听设备清单成本总额
   EventHideTable("u_equipment_list_total_cost",cntTotal)
   // 监听深化收入
   EventHideTable("u_detailed_design_revenue", cntTotal)
   // 监听深化成本总额
   EventHideTable("u_detailed_total_cost", cntTotal)
   function EventHideTable(table, fun){
    setInterval(()=>{
        if ($NG.getCmpApi("p_form_detailed_design")) {
            const _num = Number(headerTable.getItem(table).getValue())
            if(_map[table] !== _num){
                console.log("自定义监听发生变化，变化字段:  ", table);
                fun()
                _map[table] = _num
            } else {
            }
        }
       },1000)
   }
//    // 改变合同和深化成本 测试用
//    setInterval(()=>{
//     $NG.updateState((updater)=>{
//         updater.data.p_form_detailed_design.setProps({
//             u_cnt_revenue:new Date().getSeconds()*100+1000,
//             u_equipment_list_total_cost:new Date().getSeconds()*100
//         })
//     })
//    },3000)
//    setInterval(()=>{
//     $NG.updateState((updater)=>{
//         updater.data.p_form_detailed_design.setProps({
//             u_detailed_design_revenue:new Date().getSeconds()*100+1000,
//             u_detailed_total_cost:new Date().getSeconds()*100
//         })
//     })
//    },4000)

//    useValuesChange(({args})=>{
//     NucleicAcid()
//    },"p_form_detailed_design.u_equipment_list_total_cost")
   // Test - end

    //根据项目筛选合同
    function filterCntMain(){
        console.log(`项目id`,headerTable.getItem("phid_pc").getValue());
        if(!headerTable.getItem("phid_pc").getValue()) return 
        $NG.updateUI((updater, state) => {
            console.log('过滤参数检查', updater);
            updater.fieldSetForm.p_form_detailed_design.u_cnt_name.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = $NG.getCmpApi('p_form_detailed_design').getItem('phid_pc').getValue();
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                    } else {
                        return true;
                    }
                },
                ...(!!headerTable.getItem("phid_pc").getValue() && {
                    clientSqlFilter: {
                        bill_type: 4,//合同类型
                        phid_pc: headerTable.getItem("phid_pc").getValue()
                    }
                }),
            });
        });
    }
    // 获取项目信息
    function getProject(){
        $NG.execServer("getPtInfoByPhid", {phid:headerTable.getItem('phid_pc').getValue()},function(res){
            const datas = JSON.parse(res.data)[0].extendObjects
            console.log(`项目信息获取`,datas);
            $NG.updateState((updater)=>{
                updater.data.p_form_detailed_design.setProps({
                    u_sqgw: {//售前顾问 y
                        value: datas.user_sqgw_phid,
                        label: datas.user_sqgw_name,
                    },
                    u_project_manager: {//项目经理 y
                        value: datas.project_manager_phid,
                        label: datas.project_manager_name,
                    },
                    u_customer_manager: {//项目经理 y
                        value: datas.project_manager_phid,
                        label: datas.project_manager_name,
                    },
                    u_division: {//事业部 y
                        value: datas.phid_org_phid,
                        label: datas.phid_org_name,
                    },
                    u_regional_center: {//区域中心 y
                        value: datas.regional_center_phid,
                        label: datas.regional_center_name,
                    }
                })
            })
        })
    }
    //合同信息获取
    function getCntInfo(){
        $NG.execServer("getCntMInfoByPhid", {phid: headerTable.getItem("u_cnt_name").getValue()}, function (res){
            const datas= JSON.parse(res.data)[0].extendObjects
            console.log(`合同信息获取`, datas);
            $NG.updateState((updater)=>{
                updater.data.p_form_detailed_design.setProps({
                    u_sjfdw:{
                        label:datas.supply_name,
                        value: datas.supply_id
                    },
                     // 产品线 y
                     u_product_line: {
                         value: datas.midwife_phid,
                         label: datas.midwife_name,
                     },
                     // 客户名称 y
                     u_customer: {
                         value: datas.customer_id,
                         label: datas.customer_name,
                     },
                     u_cnt_revenue:datas.cnt_sum_vat_fc
                })
            })
        })
    }

});